from django.db import models
from django.conf import settings

class K8SOperation(models.Model):
    OPERATION_CHOICES = (
        ('create_namespace', '创建命名空间'),
        ('delete_namespace', '删除命名空间'),
        ('update_namespace', '更新命名空间'),
        ('get_namespace', '查询命名空间'),
    )
    
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="用户")
    operation_type = models.CharField(max_length=20, choices=OPERATION_CHOICES, verbose_name="操作类型")
    namespace_name = models.CharField(max_length=100, verbose_name="命名空间名称")
    request_data = models.JSONField(blank=True, null=True, verbose_name="请求数据")
    response_data = models.JSONField(blank=True, null=True, verbose_name="响应数据")
    success = models.BooleanField(default=False, verbose_name="是否成功")
    error_message = models.TextField(blank=True, null=True, verbose_name="错误信息")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    
    class Meta:
        verbose_name = 'K8S操作记录'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']
    
    def __str__(self):
        return f"{self.get_operation_type_display()} - {self.namespace_name}"